<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class PositionApplyController extends BaseController {
	
    public function index(){
    	$this->assign('page_title','职位申请列表');
    	$userModel = D("Users");
    	$list = $userModel->select();
    	$this->assign('list',$list);
    	$this->display();
    }

    //Ajax获取数据
    public function getData()
    {
        header("Content-type:text/html;charset=utf-8");
        $DataModel = M('Position_apply_recorder');
        $items = array();
        $page = (int)$_GET['page'];
        $items['page'] = $page>0 ? $page :1;
        $items['records'] = $DataModel->count();
        $limit = $_GET['rows'] ? (int)$_GET['rows']: 20;
        $items['total'] = ceil($items['records']/$limit);
        $list=$DataModel->field('record_id,user_id,pos_id,apply_time')->page($page)->limit($limit)->order('record_id desc')->select();
        foreach($list as $k=>$v){
            //查职位信息
            $position_info=M('Positions')->field('name,zdxl,status,num')->where('pos_id='.$v['pos_id'])->find();
            $list[$k]['position_name']=$position_info['name'];
            $list[$k]['position_zdxl']=$position_info['zdxl'];
            $list[$k]['position_status']=$position_info['status'];
            $list[$k]['position_num']=$position_info['num'];
            //查个人信息
            $user_info=M('Users')->field('name,xueli')->where('id='.$v['user_id'])->find();
            $list[$k]['user_name']=$user_info['name'];
            $list[$k]['user_xueli']=$user_info['xueli'];
            $list[$k]['user_xinzi']=M('Users_jobs')->where('user_id='.$v['user_id'])->getField('xinzi');
            //转换日期格式
            $list[$k]['apply_time']=date('Y-m-d',$v['apply_time']);
        }
        //dump($list);exit;

        //$DataModel = D("Positions");
        //$list = $DataModel->select();
        $items['rows'] = $list;
    	$this->ajaxReturn($items);
    }

    //反馈
    public function feedback(){
        if(!empty($_POST)) {
            M('Position_apply_recorder')->where("record_id=".(int)$_POST['record_id'])->save($_POST);
            header("Content-type: text/html; charset=utf-8");
            echo "<script>window.location.href='".__APP__."/index.php/PositionApply/';</script>";exit;
        }else{
            $job_info=M('Position_apply_recorder')->where('record_id='.(int)$_GET['record_id'])->limit(0,1)->find();
            $position_info=M('Positions')->field('name,zdxl,num,create_time')->where('pos_id='.$job_info['pos_id'])->find();
            $job_info['position_name']=$position_info['name'];
            $job_info['position_xueli']=$position_info['zdxl'];
            $job_info['position_num']=$position_info['num'];
            $job_info['position_create_time']=$position_info['create_time'];
            $user_info=M('Users')->field('name,xueli')->where('id='.$job_info['user_id'])->find();
            $job_info['user_name']=$user_info['name'];
            $job_info['user_xueli']=$user_info['xueli'];
            $job_info['user_xinzi']=M('Users_jobs')->where('user_id='.$job_info['user_id'])->getField('xinzi');
            $job_info['add_time']=date('Y-m-d H:i:s',$job_info['add_time']);  //时间戳转换
            $this->assign('vo',$job_info);
            $this->display();
        }
    }

    public function add(){

        $PositionModel = D("Positions");

        if(!empty($_POST)) {
            $_POST['create_time'] = date('Y-m-d H:i:s');
            $_POST['update_time'] = date('Y-m-d H:i:s');

            $data = $PositionModel->create($_POST);
            $insert_id = $PositionModel->add($data);
            if($insert_id){
                header("Content-type: text/html; charset=utf-8");
                echo "<script>window.location.href='".__APP__."/index.php/Positions/';</script>";exit;
            }
        }

        $this->display();

    }

    public function edit(){
        $DataModel = D("Positions");
        if(!empty($_POST)) {
            $_POST['update_time'] = date('Y-m-d H:i:s');
            $DataModel->where("pos_id=".(int)$_POST['id'])->save($_POST);

            header("Content-type: text/html; charset=utf-8");
            echo "<script>window.location.href='".__APP__."/index.php/Positions/';</script>";exit;
        }else{
            $item = $DataModel->where("pos_id=".(int)$_GET['id'])->select();
            $this->assign('vo',$item[0]);
            $this->display();
        }
    }

    //单条记录删除
    public function delete(){
        if(!empty($_POST)) {
            $job_id=intval($_POST['job_id']);
            $result = M('UsersJobs')->delete($job_id);
            if($result){
                header("Content-type: text/html; charset=utf-8");
                echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
            }
        }
        $this->display();
    }


}